#ifndef DOWNLOADTHREADMANAGER_H
#define DOWNLOADTHREADMANAGER_H

#include <QObject>
#include "downloadthread.h"

#define THREADCOUNT 3

class QFile;

class DownloadThreadManager : public QObject
{
    Q_OBJECT
public:
    explicit DownloadThreadManager(QObject *parent = 0);
    bool downloadFile(const QString& url);

    DownloadThread::DownloadState getState()
                    const { return m_state;}

signals:
    void downloadingFinished();
    void stateChanged(const QString&);

    void progressChange(qint64, qint64);
    void updateFileInfo(const QString&, qint64);

public slots:
    void pause();
    void restart();

private slots:
    void finishedSlot(int index);
    void errorSlot(int index, QString errorString);
    void progressChangeSlot();

private:
    void downloadingFinish();
    qint64 getFileSize(const QString& url, int tryTimes = 3);

    int m_runningCount;
    qint64 m_readySize, m_totalSize;
    QFile *m_file;
    DownloadThread::DownloadState m_state;
    QList<DownloadThread*> m_threads;

};

#endif // DOWNLOADTHREADMANAGER_H
